package com.abe.mvp.view.activity;

import java.util.Random;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.abe.mvp.contact.SocketContact;
import com.abe.mvp.presenter.SocketPresenterImp;
import com.abe.mvp.view.activity.base.BaseActivity;
import com.abe.threadmill.R;
import com.abe.utils.ToastUtils;

@ContentView(R.layout.activity_test_socket)
public class SocketActivity extends BaseActivity implements SocketContact.View {
	private SocketContact.Presenter socketPresenter;

	@ViewInject(R.id.show_exercise_button_back)
	private LinearLayout show_exercise_button_back;
	@ViewInject(R.id.show_exercise_text_title)
	private TextView show_exercise_text_title;

	@ViewInject(R.id.socket_conn)
	private Button socket_conn;
	@ViewInject(R.id.socket_send)
	private Button socket_send;

	@ViewInject(R.id.inputIP1)
	private EditText inputIP1;
	@ViewInject(R.id.inputIP2)
	private EditText inputIP2;
	@ViewInject(R.id.inputIP3)
	private EditText inputIP3;
	@ViewInject(R.id.inputIP4)
	private EditText inputIP4;

	@ViewInject(R.id.inputPORT)
	private EditText inputPORT;

	private String IP;
	private int Port;

	/** run<<>>用户名<<>>累积卡路里<<>>速度<<>>时间<<>>坡度<<>>心律<<>>头像url */
	private String sendMessage[] = {
			"<<>>120<<>>30<<>>00:20:15<<>>5<<>>75<<>>",
			"<<>>170<<>>25<<>>00:30:25<<>>10<<>>85<<>>",
			"<<>>220<<>>41<<>>00:40:15<<>>15<<>>95<<>>",
			"<<>>420<<>>20<<>>00:60:15<<>>25<<>>105<<>>",
			"<<>>820<<>>34<<>>00:60:25<<>>35<<>>115<<>>" };

	class MyHandler extends Handler {
		public MyHandler(Looper looper) {
			super(looper);
		}

		public void handleMessage(Message msg) {
			ToastUtils.showShort(getApplicationContext(), msg.obj.toString());
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		socketPresenter.closeSocket();
		socketPresenter = null;
	}

	private void initView() {
		socketPresenter = new SocketPresenterImp(this);
	}

	@Event(value = R.id.show_exercise_button_back, type = View.OnClickListener.class)
	private void buttonBack(View view) {
		back();
	}

	@Event(value = R.id.socket_conn, type = View.OnClickListener.class)
	private void connSocket(View view) {
		String ip1, ip2, ip3, ip4, port;
		ip1 = getEditString(inputIP1);
		ip2 = getEditString(inputIP2);
		ip3 = getEditString(inputIP3);
		ip4 = getEditString(inputIP4);
		port = getEditString(inputPORT);
		if (!ip1.equals("") && !ip2.equals("") && !ip3.equals("")
				&& !ip4.equals("") && !port.equals("")) {
			StringBuilder builder = new StringBuilder();
			IP = builder.append(ip1).append(".").append(ip2).append(".")
					.append(ip3).append(".").append(ip4).toString();
			Port = Integer.parseInt(port);
			socketPresenter.openSocket(IP, Port);
		}
	}

	@Event(value = R.id.socket_send, type = View.OnClickListener.class)
	private void sendMsgSocket(View view) {

		/** run<<>>用户名<<>>累积卡路里<<>>速度<<>>时间<<>>坡度<<>>心律<<>>头像url */
		String sendMessageRandom = "run<<>>"
				+ current_user.getUser().getUserName()
				+ sendMessage[new Random().nextInt(5)]
				+ current_user.getUser().getHeadimgurl();
		System.out.println(sendMessageRandom);
		socketPresenter.sendSocketCommand(sendMessageRandom);
	}

	private String getEditString(EditText editText) {
		return editText.getText().toString().trim();
	}

	@Override
	public void onSocketConnectSuccess() {
		showToast(R.string.socket_open_succ);

	}

	@Override
	public void onSocketSendDataSuccess() {
		showToast(R.string.socket_send_succ);
	}

	@Override
	public void onSocketReceiveDataSuccess(String receiveMsg) {

	}

	@Override
	public void onSocketError(String errorMsg) {
		showToast(errorMsg);
	}

	/** Handler快捷 */
	protected void localSendMessage(Message message) {
		new MyHandler(Looper.getMainLooper()).sendMessage(message);
	}

	@Override
	public void onSocketCloseSuccess() {
		showToast(R.string.socket_close_succ);
	}
}
